/*
 * Lianhaidong <lianhaidong@gmail.com>
 */


#ifndef __EASY_CONFIG_H
#define __EASY_CONFIG_H

#include <pthread.h>
#include "utils/list.h"

enum easy_config_type {
	EASY_CONFIG_DIGIT,
	EASY_CONFIG_STRING,
	EASY_CONFIG_CHAR,
	EASY_CONFIG_BOOL,
	EASY_CONFIG_CONTAINER,
};

struct easy_config_node {
	struct list_head link;
	struct list_head sub_head;
	enum easy_config_type type;
	
	int layer;
	void *data;	
	char *name;

	pthread_mutex_t lock;
};


struct easy_config_node *load_config_from_file(char *file_name);
int save_config_to_file(struct easy_config_node *node, char *file_name);

void easy_config_release(struct easy_config_node *node);

int easy_config_set_val(struct easy_config_node *node, void *val);
int easy_config_get_val(struct easy_config_node *node, void *val);

struct easy_config_node *easy_config_get_node(struct easy_config_node *father, char *name);

int easy_config_add_sub(struct easy_config_node *node, enum easy_config_type type, char *name, void *val);
int easy_config_del_sub(struct easy_config_node *node, char *name);

#define easy_config_for_each_sub(node, sub)	\
	list_for_each_entry(sub, &node->sub_head, link)

#define easy_config_for_each_sub_safe(node, sub, temp)	\
	list_for_each_entry_safe(sub, temp, &node->sub_head, link)

#define easy_config_for_each_sub_type(node, sub, type)	\
	easy_config_for_each_sub(node, sub)	\
		if (sub->type == type)

#endif	/* __EASY_CONFIG_H */
